<template>
  <div id="dialogAddEdit">
    <el-dialog
      :title="showTitle"
      :visible.sync="dialogData.isShow"
      width="480px"
      :before-close="labelHandleClose"
    >
      <div class="form-box">
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="60px"
          class="demo-ruleForm"
        >
          <el-form-item label="标题" prop="title">
            <el-input v-model.trim="ruleForm.title" clearable></el-input>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-radio-group v-model="ruleForm.type">
              <el-radio
                v-for="item in statusList"
                :key="item.dictValue"
                :label="item.dictValue"
              >
                {{ item.dictLabel }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="内容" prop="contents">
            <el-input
              v-model.trim="ruleForm.contents"
              type="textarea"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remarks">
            <el-input
              v-model.trim="ruleForm.remarks"
              type="textarea"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" plain @click="cancelLabel">取 消</el-button>
        <el-button
          type="success"
          :loading="loading"
          @click="confirmLabel('ruleForm')"
          >确认</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addUpdateLog, getUpdateLog, editUpdateLog } from '@/api/main' // 接口
export default {
  name: 'DialogAddEdit',
  msg: '字典管理-新增/编辑弹窗',
  props: ['dialogData'],
  data() {
    return {
      isAdd: true, // 新增-true,编辑-false
      showTitle: '',
      loading: false,
      statusList: [
        { dictValue: 'NEW_FEATURES', dictLabel: '新功能' },
        { dictValue: 'BUG_FIXES', dictLabel: 'BUG修复' },
        { dictValue: 'BREAKING_CHANGES', dictLabel: '功能点改变' }
      ], // 更新类型
      id: null,
      ruleForm: {
        title: '', // 标题
        type: '', // 类型
        contents: '', // 内容
        remarks: '' //备注
      },
      rules: {
        title: [
          { required: true, message: '更新标题不能为空', trigger: 'blur' }
        ],
        type: [{ required: true, message: '请选择类型', trigger: 'blur' }],
        remarks: {}
      }
    }
  },
  mounted() {
    this.id = this.dialogData.id
    this.showTitle = this.id ? '编辑更新日志' : '新增更新日志'
    if (this.id) {
      // 编辑
      this.getFormData()
      this.isAdd = false
    }
  },
  methods: {
    /* 编辑时获取表单详情 */
    getFormData() {
      getUpdateLog(this.id, (response) => {
        let { contents, remarks, title, type } = response
        this.ruleForm = {
          title,
          type,
          contents,
          remarks
        }
      })
    },
    labelHandleClose(done) {
      this.cancelLabel()
      done()
    },
    cancelLabel() {
      // 关闭前清空
      this.loading = false
      this.id = null
      this.dialogData.id = null
      this.dialogData.isShow = false
    },
    /* 确认提交 */
    confirmLabel(formName) {
      /* let _this = this*/
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.subForm()
          /* Promise.all([_this.checkdictType()]).then((datas) => {
            _this.subForm()
          })*/
        } else {
          return false
        }
      })
    },
    /* 提交表单 */
    subForm() {
      this.loading = true
      let obj = { ...this.ruleForm }
      if (this.isAdd) {
        // 新增
        addUpdateLog(
          obj,
          (response) => {
            this.loading = false
            this.$emit('refresh')
            this.cancelLabel()
          },
          (response) => {
            this.loading = false
            this.$message.warning(this.showTitle + '失败，' + response.message)
          }
        )
      } else {
        // 编辑
        editUpdateLog(
          this.id,
          obj,
          (response) => {
            this.loading = false
            this.$emit('refresh')
            this.cancelLabel()
          },
          (response) => {
            this.loading = false
            this.$message.warning(this.showTitle + '失败，' + response.message)
          }
        )
      }
    }
  }
}
</script>

<style lang="less">
// #dialogAddEdit {
// }
</style>
